<?php
session_start();
require_once ('../init.php');
require_once ('includes/lib/func_rightCtrl.php');
// 权限控制
admin_rightCtrl();
if($_SESSION['level'] == 0){
	require_once ('includes/views/root_header.php');
}
elseif ($_SESSION['level'] == 1) {
	require_once ('includes/views/admin_header.php');
}
// 换页部分处理
$playerNum_query = "SELECT `uid` FROM `user`";
$sth_playernum   = $dbh -> prepare($playerNum_query);
$sth_playernum	 ->execute();
$playernum 	     = $sth_playernum->rowCount();
$page = @$_GET['page'];
if (!isset($page)){
	$page = 1;
}
// 首页
$first_page = 1;
// 上一页
$last_page = $page -1;
// 下一页
$next_page = $page + 1;
// 尾页
$trailer_page = (int)($playernum / 20) + 2;
// 如果上一页超出了首页，设为首页
if($page <= $first_page){
	$page = $first_page;
	$last_page = $first_page;
}
// 如果下一页超出尾页，设为尾页
elseif($page >= $trailer_page){
	$page = $trailer_page;
	$next_page = $trailer_page;
}
// 配合查询语句处理后的当前页[limit从0开始]
$page = $page - 1;
// 传入查询语句的页面换算值
$pageValue = $page * 20;

$query   = "SELECT `uid`,`name`,`nickname`,`email`,`skilled`,`last_time`,`totalpoint`,`rank` FROM `user` ORDER BY `rank` ASC limit :page,20";
$sth     = $dbh->prepare($query);
$sth -> bindParam(":page",$pageValue,PDO::PARAM_INT);
$sth -> execute();
$rownum = $sth->rowCount();
//表格头
echo <<<EOT
<link href="/afctf/css/announce.css" rel="stylesheet" type="text/css" media="all"/>
<div class="challenge_table">
	<blockquote class="layui-elem-quote">选手信息</blockquote>
	<table border="1" class="layui-table">
		<thead>
		  <tr align="center">
		    <th>学号</th>
		    <th>姓名</th>
		    <th>昵称</th>
		    <th>邮箱</th>
		    <th>强项</th>
		    <th>最近一次得分时间</th>
		    <th>总分数</th>
		    <th>排行</th>
		  </tr>
		</thead>
		<tbody>
EOT;
if($rownum === 0){
	echo <<<EOT
	<tr align="center">
	    <td colspan="8">暂无数据</td>
	</tr>
EOT;
}
else{
	//表格内容
	for($i = 0;$i < $rownum; $i++){
		$result  = $sth -> fetch();
		$uid = $result['uid'];
		$name = $result['name'];
		$nickname = $result['nickname'];
		$email = $result['email'];
		$skilled = $result['skilled'];
		$last_time = $result['last_time'];
		$totalpoint = $result['totalpoint'];
		$rank = $result['rank'];
		echo <<<EOT
		  <tr align="center">
		    <td><a href="/afctf/admin/playerInfo.php?uid=$uid">$uid</a></td>
		    <td>$name</td>
		    <td>$nickname</td>
		    <td>$email</td>
		    <td>$skilled</td>
		    <td>$last_time</td>
		    <td>$totalpoint</td>
		    <td>$rank</td>
		  </tr>
EOT;
	}
}
//表格尾
echo <<<EOT
		</tbody>
	</table>
	<div id="page">	
		<button class="layui-btn" onclick="jump($first_page)">
			首页
		</button>
		<button class="layui-btn" onclick="jump($last_page)">
			上一页
		</button>
		<button class="layui-btn" onclick="jump($next_page)">
			下一页
		</button>
		<button class="layui-btn" onclick="jump($trailer_page)">
			尾页
		</button>
		<input type="text" placeholder="页号" id="SetPage" size="5" style="margin-left:10px"></input>
		<button onclick="skip()" class="layui-btn skip" style="margin-left:10px">
			 	跳转
		</button>
	</div>
</div>
<script type="text/javascript">
	$("#a_home").removeClass("active");
	$("#a_player").addClass("active");
</script>
<script type="text/javascript">
    function jump(page){
        self.location.href="?action=rank&page=" + page;
    }
    function skip(){
    	var regPos = /^\d+$/;
    	if(SetPage.value == ""){
    		alert("请输入页号!");
    	}
    	else if(!regPos.test(SetPage.value)){
			self.location.href="?action=rank&page=" + $first_page;
    	}
    	else if(SetPage.value < $first_page || SetPage.value > $trailer_page){
    		self.location.href="?action=rank&page=" + $first_page;
    	}
    	else{
    		self.location.href="?action=rank&page=" + SetPage.value;
    	}
    }
</script>
EOT;
require_once ('includes/views/footer.php');
?>